import { courseGet } from '@/api/course';
import ButtonAuth from '@/auth/ButtonAuth';
import { EllipsisOutlined, PlusOutlined } from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable, TableDropdown } from '@ant-design/pro-components';
import { Button, Dropdown, Image } from 'antd';
import { useRef } from 'react';
// import request from 'umi-request'; //axios同类产品
export const waitTimePromise = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export const waitTime = async (time: number = 100) => {
  await waitTimePromise(time);
};

type CourseType = {
  name:string
  isvip:number
  poster:string
  createdAt:string
};

const columns: ProColumns<CourseType>[] = [
  {
    dataIndex: 'index',
    valueType: 'indexBorder',
    width: 48,
  },
  {
    title: '课程名称',
    dataIndex: 'name',
    copyable: true,
    ellipsis: true,
    tip: '标题过长会自动收缩'
  },
  {
    disable: true,
    title: '是否收费',
    dataIndex: 'isvip',
    filters: true,
    onFilter: true,
    ellipsis: true,
    valueType: 'select', //讲当前列设置为下拉选择筛选的模式
    valueEnum: {
      1: { text: '全部课程'},
      2: { text: '免费课程'},
      3: { text: '会员课程'},
    },
    // search:false
  },
  {
    // disable: true,
    title: '课程封面',
    dataIndex: 'poster',
    search: false,
    render: (poster:any, _:CourseType) => {
      return <Image src={poster}/>
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    dataIndex: 'createdAt',
    valueType: 'date',
    sorter: true,
    hideInSearch: true,
  },
  {
    title: '操作',
    valueType: 'option',
    key: 'option',
    render: (_text, _record, _, action) => [
      <a
        key="editable"
      >
        编辑
      </a>,
      <a target="_blank" rel="noopener noreferrer" key="view">
        查看
      </a>,
      <TableDropdown
        key="actionGroup"
        onSelect={() => action?.reload()}
        menus={[
          { key: 'copy', name: '复制' },
          { key: 'delete', name: '删除' },
        ]}
      />,
    ],
  },
];

const Course = () => {
  const actionRef = useRef<ActionType>();
  return (
    <ProTable<CourseType>
      columns={columns}
      actionRef={actionRef}
      cardBordered
      request={async ({name,isvip}, sort, filter) => {
        console.log('params',name,isvip);
        console.log('sort',sort);
        console.log('filter',filter);
        let res = await courseGet({name,isvip})
        return {
          data:res.data.results,
          success:true
        }
      }}
      editable={{
        type: 'multiple',
      }}
      columnsState={{
        persistenceKey: 'pro-table-singe-demos',
        persistenceType: 'localStorage',
        defaultValue: {
          option: { fixed: 'right', disable: true },
        },
        onChange(value) {
          console.log('value: ', value);
        },
      }}
      rowKey="id"
      search={{
        labelWidth: 'auto',
      }}
      options={{
        setting: {
          listsHeight: 400,
        },
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values, type) => {
          if (type === 'get') {
            return {
              ...values,
              created_at: [values.startTime, values.endTime],
            };
          }
          return values;
        },
      }}
      pagination={{
        pageSize: 5,
        onChange: (page) => console.log(page),
      }}
      dateFormatter="string"
      headerTitle="高级表格"
      toolBarRender={() => [
        <ButtonAuth permit={['超级管理员','管理员']}>
          <Button
            key="button"
            icon={<PlusOutlined />}
            onClick={() => {
              actionRef.current?.reload();
            }}
            type="primary"
          >
            新建
          </Button>
        </ButtonAuth>,
        <Dropdown
          key="menu"
          menu={{
            items: [
              {
                label: '1st item',
                key: '1',
              },
              {
                label: '2nd item',
                key: '1',
              },
              {
                label: '3rd item',
                key: '1',
              },
            ],
          }}
        >
          <Button>
            <EllipsisOutlined />
          </Button>
        </Dropdown>,
      ]}
    />
  );
};

export default Course